package com.thz.houserental.domain.reservation;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thz.houserental.infrastructure.auth.AuthUtil;
import com.thz.houserental.infrastructure.table.User;
import com.thz.houserental.infrastructure.web.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/reservation")
@Tag(name = "房屋预定管理",
        description = "reservation(预约), /add 添加一条喜欢记录, /collect 将一条喜欢记录加到收藏成为真正的预约(isCollect = 1)")
public class ReservationController {
    private final ReservationService reservationService;
    private final UserService userService;

    public ReservationController(ReservationService reservationService, UserService userService) {
        this.reservationService = reservationService;
        this.userService = userService;
    }

    @Operation(summary = "添加喜欢")
    @PostMapping("/add")
    @Transactional
    public Reservation addReservation(@RequestParam @Parameter(description = "房屋id") String houseId) {
        return this.reservationService.addReservation(houseId);
    }

    @Operation(summary = "添加收藏")
    @PutMapping("/collect")
    @Transactional
    public void addCollect(@RequestParam @Parameter(description = "房屋id") String houseId) {
        this.reservationService.collect(houseId);
    }

    @Operation(summary = "取消喜欢")
    @DeleteMapping("/cancelLike")
    public void cancelLike(@RequestParam @Parameter(description = "房屋id") String houseId) {
        QueryWrapper<Reservation> reservationQueryWrapper = new QueryWrapper<>();
        reservationQueryWrapper.lambda()
                .eq(Reservation::getHouseId, houseId)
                .eq(Reservation::getUserId, AuthUtil.getCurrentUserId());
        this.reservationService.remove(reservationQueryWrapper);
    }

    @Operation(summary = "取消收藏")
    @PutMapping("/cancelCollect")
    public void cancelCollect(@RequestParam @Parameter(description = "房屋id") String houseId) {
        QueryWrapper<Reservation> reservationQueryWrapper = new QueryWrapper<>();
        reservationQueryWrapper.lambda()
                .eq(Reservation::getUserId, AuthUtil.getCurrentUserId())
                .eq(Reservation::getHouseId, houseId);
        Reservation reservation = this.reservationService.getOne(reservationQueryWrapper);
        reservation.setIsCollect(0);
        this.reservationService.updateById(reservation);
    }


    @Operation(summary = "获得用户喜欢(包括收藏)列表")
    @GetMapping("/list")
    public Page<ReservationVo> getUserAllReservation(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "userId", defaultValue = "") String userId) {
        return this.reservationService.getUserAllReservation(userId, new Page<ReservationVo>(pageNo, pageSize));
    }

    //TODO 添加权限，super admin
    @Operation(summary = "超级管理员获取所有用户收藏")
    @GetMapping("/superList")
    public Page<ReservationVo> getAll(
            ReservationVo reservationVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        QueryWrapper<Reservation> reservationVoQueryWrapper = new QueryWrapper<>();
        reservationVoQueryWrapper
                .eq("is_collect", 1);

        Page<ReservationVo> all = this.reservationService.getAll(reservationVoQueryWrapper, new Page<>(pageNo, pageSize));
        return this.reservationService.getAllReservationPage(new Page<ReservationVo>(pageNo, pageSize), reservationVo);
    }
}
